package com.example.demo.tixiban;

/**
 * @author xujm
 * @version V2.4
 * @since 2.4.0 2022/2/10 19:46
 */
public class List_用单链表实现队列 {


    static class Node<V>{
        V val;
        Node<V> next;
        public Node(V v) {
            this.val = v;
        }
    }

    static class MyQueue<V>{
        Node<V> head;
        Node<V> tail;
        int curr;
        int size;

        public MyQueue( int size) {
            this.head = null;
            this.tail = null;
            this.curr = 0;
            this.size = size;
        }

        public void add(V v){
            if(curr == 0){
                Node node = new Node(v);
                head = node;
                tail = node;
            }else{
                tail.next = new Node(v);
                tail = tail.next;
            }
            curr++;
        }

        public V remove(){
            if(curr == 0){
                return null;
            }else{
                Node<V> temp = head;
                head = head.next;
                if(curr == 1){
                    tail = null;
                }
                curr--;
                return temp.val;
            }
        }

        public boolean isEmpty(){
            return curr == 0;
        }

    }

    public static void main(String[] args) {

        MyQueue<Integer> queue = new MyQueue<>(100);
        queue.add(1);
        queue.add(2);
        queue.add(3);
        while(!queue.isEmpty()){
            System.out.println(queue.remove());
        }

    }



}
